var express = require('express');
var router = express.Router();
var User = require('../models/user')
/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
// 登录接口
router.post('/login', (req, res, next)=> {
  var param = {
    userName: req.body.userName,
    userPwd: req.body.userPwd
  }
  console.log(param)
  User.findOne(param, (err, doc) => {
    if(err) {
      res.json({
        status: '1',
        msg: err.message
      })
    } else {
      if(doc) {
        res.cookie("userId", doc.userId, {
          path: '/',
          maxAge: 1000*60*60
        })
        res.cookie("userName", doc.userName, {
          path: '/',
          maxAge: 1000*60*60
        })
        // res.session.user
        res.json({
          status: '0',
          msg: '',
          result: {
            userName: doc.userName, 
            userPwd: doc.userPwd
          }
        })
      } else if(!doc){
        res.json({
          status: '1',
          msg: 'account error'
        })
        // console.log(res);
        
        // console.log(res.json)
      }
    }
  })
});
// 注销登录接口
router.post('/logout',(req, res, next)=>{
  res.cookie('userId','',{
    path: '/',
    maxAge: -1
  })
  res.json({
    status: '0',
    msg: '',
    result: ''
  })
})

// 检查登录接口
router.get('/checkLogin',(req,res,next) => {
  if(req.cookies.userId) {
    res.json({
      status: '0',
      msg: '',
      result: req.cookies.userName
    })
  } else {
    res.json({
      status: '1',
      msg: '未登录',
      result: ''
    })
  }
})

// 购物车数据获取接口
router.get('/cartList',(req, res, next) => {
  var userId = req.cookies.userId;
  User.findOne({userId:userId},(err, doc) => {
    if(err) {
      res.json({
        status: '1',
        msg: err.message,
        result: ''
      })
    } else {
      if(doc) {
        res.json({
          status: '0',
          msg: '',
          result: doc.cartList
        })
      }
    }
  })
})

// 购物车数据删除接口
router.post('/del',(req, res, next) => {
  var userId = req.cookies.userId ,productId = req.body.productId
  User.update({
    userId:userId
  },{
    $pull:{
      'cartList':{
        'productId':productId
      }
    }
  },(err, doc) => {
    if(err) {
      res.json({
        status: '1',
        msg: err.message,
        result: ''
      })
    } else {
      if(doc) {
        res.json({
          status: '0',
          msg: 'delete succ',
          result: ''
        })
      }
    }
  })
})

// 购物车数据编辑改变接口
router.post('/cartChange',(req,res,next) => {
  var userId = req.cookies.userId, 
      productId = req.body.productId,
      productNum = req.body.productNum,
      checked = req.body.checked
  User.update({"userId":userId,"cartList.productId":productId},{
    "cartList.$.productNum":productNum,
    "cartList.$.checked":checked
  },(err, doc) => {
    if(err) {
      res.json({
        status: '1',
        msg: err.message,
        result: ''
      })
    } else {
      if(doc) {
        res.json({
          status: '0',
          msg: '',
          result: 'succ'
        })
      }
    }
  })
})
// 全选接口
router.post('/editCheckAll',(req,res,next)=>{
  var userId = req.cookies.userId,
      checkAll = req.body.checkAll?'1':'0'
      console.log(checkAll)
  User.findOne({userId:userId},(err1,doc1)=>{
    if(err1) {
      res.json({
        status: '1',
        msg: err1.message,
        result: ''
      })
    } else {
      if(doc1) {
        doc1.cartList.forEach(element => {
          element.checked = checkAll
        })
        doc1.save((err2,doc2)=>{
          if(err2) {
            res.json({
              status: '1',
              msg: err2.message,
              result: ''
            })
          } else {
            if(doc2) {
              res.json({
                status: '0',
                msg: '',
                result: 'succ'
              })
            }
          }
        })
      }
    }
  })
})
// 地址获取接口
router.get('/address', (req,res,next) => {
  var userId = req.cookies.userId
  User.findOne({userId:userId},(err,doc)=>{
    if(err) {
      res.json({
        status: '1',
        msg: err1.message,
        result: ''
      })
    } else {
      if(doc) {
        res.json({
          status: '0',
          msg: '',
          result: doc.addressList
        })
      }
    }
  })
})
// 新增地址接口
router.post('/addAddress',{
  
})
//设置默认地址接口
router.post('/setDefaultAddr',(req,res,next) => {
  var userId = req.cookies.userId,
      addressId = req.body.addressId
  User.findOne({userId:userId},(err,doc) => {
    if(err) {
      res.json({
        status: '1',
        msg: err.message,
        result: ''
      })
    } else {
      var addressList = doc.addressList
      addressList.forEach((item) => {
        if(item.addressId == addressId) {
          item.isDefault = true
        } else {
          item.isDefault = false
        }
      })
      doc.save((err1,doc1) => {
        if(err1) {
          res.json({
            status: '1',
            msg: err1.message,
            result: ''
          })
        } else {
          res.json({
            status: '0',
            msg: '',
            result: 'succ'
          })
        }
      })
    }
  })
})
// 删除地址接口
router.post('/deleteAddress',(req,res,next) => {
  var userId = req.cookies.userId
      addressId = req.body.addressId
  User.update({userId:userId},{
    $pull:{
      'addressList':{
        'addressId':addressId
      }
    }
  },(err,doc) => {
    if(err) {
      res.json({
        status: '1',
        msg: err1.message,
        result: ''
      })
    } else {
      if(doc) {
        res.json({
          status: '0',
          msg: 'delete succ',
          result: ''
        })
      }
    }
  })
})
module.exports = router;
